/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.sys.entity.Config;
import cn.net.ecode.modules.sys.service.ConfigService;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 参数设置Controller
 * @author ThinkGem
 * @version 2014-07-31
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/config")
public class ConfigController extends BaseController {

	@Autowired
	private ConfigService configService;
	
	/**
	 * 获取数据
	 * @param id
	 * @return
	 */
	@ModelAttribute
	public Config get(String id, boolean isNewRecord) {
		return configService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 * @param config
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:config:view")
	@RequestMapping(value = {"list", ""})
	public String list(Config config, Model model) {
		return "modules/sys/configList";
	}
	
	/**
	 * 查询列表
	 * @param config
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:config:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<Config> listData(Config config, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Config> page = configService.findPage(new Page<Config>(request, response), config); 
		return page;
	}

	/**
	 * 查看编辑表单
	 * @param config
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:config:view")
	@RequestMapping(value = "form")
	public String form(Config config, Model model) {
		model.addAttribute("config", config);
		return "modules/sys/configForm";
	}

	/**
	 * 保存数据
	 * @param config
	 * @return
	 */
	@RequiresPermissions("sys:config:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(Config config) {
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, config)){
			return renderResult(Global.FALSE, message);
		}
		if (Global.YES.equals(config.getIsSys()) && !config.getCurrentUser().isSuperAdmin()){
			return renderResult(Global.FALSE, "越权操作，只有超级管理员才能修改此数据！");
		}
		configService.save(config);
		return renderResult(Global.TRUE, "保存参数成功！");
	}
	
	/**
	 * 验证关键字是否有效
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("sys:config:edit")
	@RequestMapping(value = "checkKeys")
	public String checkKeys(String oldKeys, String keys) {
		Config config = new Config();
		config.setKeys(keys);
		if (keys != null && keys.equals(oldKeys)) {
			return "true";
		} else if (keys != null && configService.get(config) == null) {
			return "true";
		}
		return "false";
	}
	
	/**
	 * 删除数据
	 * @param config
	 * @return
	 */
	@RequiresPermissions("sys:config:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(Config config, RedirectAttributes redirectAttributes) {
		if (!UserUtils.getUser().isSuperAdmin() && Global.YES.equals(config.getIsSys())){
			return renderResult(Global.FALSE, "越权操作，只有超级管理员才能修改此数据！");
		}
		configService.delete(config);
		return renderResult(Global.TRUE, "删除参数成功！");
	}

}